



//
//  ZSubLikeViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/5/7.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZSubLikeViewController.h"
#import "LikeCell.h"
#import "ReadModel.h"
@interface ZSubLikeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger index;

@end
#define cellIdentifier @"like"
@implementation ZSubLikeViewController
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 3, ScreenWidth , ScreenHeight - kTabHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 95.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"LikeCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        //        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.index = -1;
    self.dataArray = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadModel *model = self.dataArray[indexPath.row];
    LikeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.nameLab.text = model.bookInfo[@"name"];
    cell.timeLab.text = model.createTime;
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.bookInfo[@"photo"]]] placeholderImage:nil];
    return cell;
}

#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    YYLog(@"%ld",index);
    //    [UserModel shareInstance].tabIndex = index;
    //    self.currentIndexId = index;
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
    //    self.currentIndex = index;
    //    [self loadDataWithIndex:index];
    //    [self loadDataWithIndex:[UserModel shareInstance].tabIndex];
        [self loadDataWithIndex:index];
}


-(void)loadDataWithIndex:(NSInteger)index{
    
    [self.dataArray removeAllObjects];
    NSString *userid = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    YYLog(@"%@",userid);
    
    NSDictionary *dic;
    //    if (index==0) {
    ///一定要弄清赋值和等于的不同，index==0  注意
    ///type  1 资讯    2视频    3图书
    if (index==0) {
        //资讯
        dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"userInfo.id":userid,@"type":@"1",};
        
    }else if (index==1){
        //视频
        dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"userInfo.id":userid,@"type":@"2",};
    }else{
        ///图书
        dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"userInfo.id":userid,@"type":@"3",};
    }
    
    //    }
    [ZNetworkService postRequestWithBaseUrl:kLikeUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }
        
    } fail:^(NSError *error) {
        
    } delegater:nil];
}

@end
